package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"kuangstudy-mall/srvs/user-srv/proto"
	"time"
)

var userClient proto.UserClient

func InitClient() {
	conn, err := grpc.Dial("127.0.0.1:9000", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	userClient = proto.NewUserClient(conn)
}

/**
 * 测试用户列表
 * @author feige
 * @date 2024-04-08
 * @version 1.0
 * @desc
 */
func TestFindUserInfoList() {
	pageInfo := proto.PageInfo{PageNo: 2, PageSize: 2}
	userListResponse, err := userClient.FindUserList(context.Background(), &pageInfo)
	if err != nil {
		panic(err)
	}
	fmt.Println(userListResponse.Total)
	fmt.Println(userListResponse.Data)
}

/**
 * 测试用户列表
 * @author feige
 * @date 2024-04-08
 * @version 1.0
 * @desc
 */
func TestGetUserByTelephone() {
	request := proto.TelephoneRequest{Telephone: "18782224220"}
	userInfoResponse, err := userClient.GetUserByTelephone(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(userInfoResponse)
}

/**
 * 测试用户列表
 * @author feige
 * @date 2024-04-08
 * @version 1.0
 * @desc
 */
func TestGetUserById() {
	request := proto.IdRequest{Id: 33}
	userInfoResponse, err := userClient.GetUserById(context.Background(), &request)
	if err != nil {
		panic(err)
	}
	fmt.Println(userInfoResponse)
}

/**
 * 密码检测
 * @author feige
 * @date 2024-04-08
 * @version 1.0
 * @desc
 */
func TestCheckPassword() {
	passwordCheckRequest := proto.PasswordCheckRequest{
		Password:       "123456",
		EncodePassword: "$pbkdf2-sha512$FWDWshVDS3SdH4Wm$8f28ed2477c1f4d00f7b443d3256cc1a4fc78122e05d7b961cae88338d9df940",
	}
	response, err := userClient.CheckPassword(context.Background(), &passwordCheckRequest)
	if err != nil {
		panic(err)
	}
	fmt.Println(response.Success)
}

/**
 * 保存接口
 * @author feige
 * @date 2024-04-08
 * @version 1.0
 * @desc
 */
func TestSaveUser() {
	createUserRequest := proto.CreateUserRequest{
		NickName: "飞哥",
		PassWord: "123456",
		BirthDay: uint64(time.Now().Unix()),
		Gender:   "male",
		Mobile:   "15057845454",
	}
	response, err := userClient.SaveUser(context.Background(), &createUserRequest)
	if err != nil {
		panic(err)
	}
	fmt.Println(response)
}

func TestLogin() {
	//登录解密的时候
	//- A 输入账号，根据账号查询出每个用户自己盐和加密的密码
	//- 用户输入的密码 + 每个用户自己盐 ===加密====真实加密密码
	//- 真实加密密码 和 数据库的密码去比较 如果相等就说明密码是正确的，反之输入密码有误，
	inputMobile := "15057845454"
	inputPassword := "123456"

	telephoneRequest := proto.TelephoneRequest{
		Telephone: inputMobile,
	}
	userInfoResponse, err := userClient.GetUserByTelephone(context.Background(), &telephoneRequest)
	if err != nil {
		panic(err)
	}

	passwordCheckRequest := proto.PasswordCheckRequest{
		Password:       inputPassword,
		EncodePassword: userInfoResponse.PassWord,
	}
	checkResponse, err := userClient.CheckPassword(context.Background(), &passwordCheckRequest)
	if err != nil {
		panic(err)
	}
	fmt.Println(checkResponse)
}

func main() {
	InitClient()
	//TestFindUserInfoList()
	//TestGetUserByTelephone()
	//TestGetUserById()
	TestCheckPassword()
	//TestSaveUser()
}
